package com.king.cms.preview;

import java.io.File;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.InstantiationException; 

import com.king.cms.action.BaseDispatchAction;
import com.king.cms.common.Constants;
import com.king.cms.common.FileDoc;
import com.king.cms.common.FileRw;
import com.king.cms.common.FileUp;
import com.king.cms.log.LogService;
import com.king.cms.util.page.JpegTool;
import com.king.cms.util.page.ZoomImage;
 

public class PreviewAction extends BaseDispatchAction{
	private static final Log  log = LogFactory.getLog(PreviewAction.class);
	/**
	 * Method execute
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws ServletException 
	 * @throws UnsupportedEncodingException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @throws java.lang.InstantiationException 
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws ServletException, UnsupportedEncodingException {
	    String type = request.getParameter("type"); 
	    request.setCharacterEncoding("UTF-8");
	    if(Constants.INFO_TEMPLATE.equals(type)){
	    	String filePath = request.getParameter("filePath");
	    	String hytzLine1 = request.getParameter("hytzLine1");
	    	 
	    	String hytzLine2 = request.getParameter("hytzLine2");
	    	String hytzLine3 = request.getParameter("hytzLine3");
	    	String hytzLine4 = request.getParameter("hytzLine4");
	    	String hytzKssj = request.getParameter("hytzKssj");
	    	String hytzJssj = request.getParameter("hytzJssj");
	    	String width = request.getParameter("width");
	    	String height = request.getParameter("height");
	    	String lcdId = request.getParameter("lcdId");
	    	String meetingId = request.getParameter("meetingId");
	    	List template = null;
	    	
	    	if(meetingId == null||"null".equals(meetingId)){
	    		 
	    	    template = this.getDBOperator(request).select("select * from cms_delphi_template where id = (select DISPLAY_TEMPLATE from cms_client where id = ?)", new Object[]{lcdId});
	    	}else{
	    	    template = this.getDBOperator(request).select("select * from cms_delphi_template where id = (select TEMPLATE_ID from cms_meeting_client where CLIENT_ID = ? and MEETING_ID=?)", new Object[]{lcdId,meetingId});
	    	    if(template.size() == 0)
	    	    	template = this.getDBOperator(request).select("select * from cms_delphi_template where id = (select DISPLAY_TEMPLATE from cms_client where id = ?)", new Object[]{lcdId});
	    	}
	    	FileRw f = new FileRw();
	    	String backgrand = "";
	    	if(template.size()>0){
	    		Map m = (Map)template.get(0);
	    		backgrand = System.currentTimeMillis()+".jpg";
	    		
	    		f.saveTempFile((byte[])m.get("DELPHI_TEMPLATE_CONTENT"), request.getRealPath("/")+"uploadfile/",backgrand );
	    		try{ 

					JpegTool jpegTool = new JpegTool();  
					jpegTool.SetSmallWidth(Integer.parseInt(width)); 
					jpegTool.SetSmallHeight(Integer.parseInt(height));
					//jpegTool.doFinal(request.getRealPath("/")+FileRw.getFileSavePath("")+backgrand,request.getRealPath("/")+"//previewimage//"+backgrand); 
					ZoomImage image = new ZoomImage(Integer.parseInt(width),Integer.parseInt(height));
					image.createImage(new FileInputStream(request.getRealPath("/")+FileRw.getFileSavePath("")+backgrand), new File(request.getRealPath("/")+"//previewimage//"+backgrand));
					}catch(Exception e){ 

					e.printStackTrace(); 

					}   
	    	}
	    	request.setAttribute("hytzKssj", hytzKssj);
	    	request.setAttribute("hytzJssj", hytzJssj);
	    	request.setAttribute("filePath", filePath);
//	    	System.out.println(new String(hytzLine1.getBytes("ISO-8859-1"),"utf-8").replaceAll("@", "&"));
//	    	System.out.println(new String(hytzLine2.getBytes("ISO-8859-1"),"utf-8"));
//	    	System.out.println(new String(hytzLine3.getBytes("ISO-8859-1"),"utf-8"));
//	    	System.out.println(new String(hytzLine4.getBytes("ISO-8859-1"),"utf-8"));
	    	request.setAttribute("hytzLine1",new String(hytzLine1.getBytes("ISO-8859-1"),"utf-8").replaceAll("@", "&"));
	    	request.setAttribute("hytzLine2",new String(hytzLine2.getBytes("ISO-8859-1"),"utf-8").replaceAll("@", "&"));
	    	request.setAttribute("hytzLine3",new String(hytzLine3.getBytes("ISO-8859-1"),"utf-8").replaceAll("@", "&"));
	    	request.setAttribute("hytzLine4",new String(hytzLine4.getBytes("ISO-8859-1"),"utf-8").replaceAll("@", "&"));
	    	 
	    	request.setAttribute("fileName", backgrand);
	    	return mapping.findForward("preview");
	    }
		if(Constants.INFO_PPT.equals(type)){
			    	
		}
		if(Constants.INFO_VIDEO.equals(type)){
			
		}
		return mapping.findForward("logon.fail");
	 
	}
}
